package com.lpw.dbvideo.videoorder.provider;

import com.lpw.dbvideo.videoorder.domain.VideoOrder;
import org.apache.ibatis.jdbc.SQL;

/**
 * videoOrder 的动态sql语句
 */
public class VideoOrderProvider {

    /**
     * 新增videoOrder
     * @param videoOrder 新增的内容
     * @return
     */
    public String insert(final VideoOrder videoOrder){
        return new SQL(){
            {
                INSERT_INTO("video_order");
                if(videoOrder.getOpenid() != null){
                    VALUES("openid","#{openid}");
                }
                if(videoOrder.getOutTradeNo() != null){
                    VALUES("out_trade_no","#{outTradeNo}");
                }
                if(videoOrder.getState() != null){
                    VALUES("state","#{state}");
                }
                if(videoOrder.getCreateTime() != null){
                    VALUES("create_time","#{createTime}");
                }
                if(videoOrder.getNotifyTime() != null){
                    VALUES("notify_time","#{notifyTime}");
                }
                if(videoOrder.getTotalFee() != null){
                    VALUES("total_fee","#{totalFee}");
                }
                if(videoOrder.getNickname() != null){
                VALUES("nickname","#{nickname}");
                }
                if(videoOrder.getHeadImg() != null){
                    VALUES("head_img","#{headImg}");
                }
                if(videoOrder.getVideoId() != null){
                    VALUES("video_id","#{videoId}");
                } if(videoOrder.getVideoTitle() != null){
                    VALUES("video_title","#{videoTitle}");
                }
                if(videoOrder.getVideoImg() != null){
                    VALUES("video_img","#{videoImg}");
                }
                if(videoOrder.getUserId() != null){
                    VALUES("user_id","#{userId}");
                }
                if(videoOrder.getIp() != null){
                    VALUES("ip","#{ip}");
                }
                if(videoOrder.getDel() != null){
                    VALUES("del","#{del}");
                }
            }
        }.toString();
    }

    /**
     * 根据订单号更新订单信息
     * @param videoOrder 更新内容
     * @return
     */
    public String updateVideoOrderByOutTradeNo(final VideoOrder videoOrder){
        return new SQL(){
            {
                UPDATE("video_order");
                if(videoOrder.getState() != null){
                    SET("state= #{state}");
                }
                if(videoOrder.getNotifyTime() != null){
                    SET("state= #{state}");
                }
                if(videoOrder.getState() != null){
                    SET("notify_time= #{notifyTime}");
                }
                if(videoOrder.getOpenid() != null){
                    SET("openid= #{openid}");
                }
                WHERE("out_trade_no = #{outTradeNo} and state=0 and del = 0");
            }
        }.toString();
    }
}
